<template>
	<div>
		<!-- 面包屑导航 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>商品管理</el-breadcrumb-item>
			<el-breadcrumb-item>商品列表</el-breadcrumb-item>

		</el-breadcrumb>
		<!-- 卡片式图 -->
		<el-card class="box-card">
			<!-- 搜索添加区 -->

			<el-row :gutter="20">
				<el-col :span="8">
					<!-- @clear 绑定getuserlist 点击文本清除时重新搜索数据 -->
					<el-input @clear="getgoodslist" clearable placeholder="请输入内容" v-model="queryinfo.query"
						class="input-with-select">
						<el-button slot="append" icon="el-icon-search" @click="getgoodslist"></el-button>
					</el-input>
				</el-col>
				<el-col :span="4">
					<el-button type="primary" @click="goAddpage">添加商品</el-button>
				</el-col>
			</el-row>
			<!-- 用户列表区 -->
			<el-table border stripe :data="goodslist" style="width: 100%">
				<el-table-column type="index" label="#">
				</el-table-column>
				<el-table-column prop="goods_name" label="商品名称" width="180">
				</el-table-column>
				<el-table-column prop="goods_price" label="商品价格(元)" width="180">
				</el-table-column>
				<el-table-column prop="goods_weight" label="商品重量">
				</el-table-column>
				<el-table-column prop="add_time" label="创建时间">
					<template slot-scope="scope">
						{{scope.row.add_time | dateFormat}}
					</template>
				</el-table-column>

				<el-table-column label="操作">
					<template slot-scope="scope">
						<!-- 修改按钮 -->
						<el-button type="primary" icon="el-icon-edit" size="mini"
							@click="showEditDialog(scope.row.goods_id)">
						</el-button>
						<!-- 删除按钮 -->
						<el-button type="danger" icon="el-icon-delete" size="mini"
							@click="removegoodsById(scope.row.goods_id)"></el-button>


					</template>

				</el-table-column>
			</el-table>

			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="queryinfo.pagenum" :page-sizes="[2,5,10]" :page-size="queryinfo.pagesize"
				layout="total, sizes, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</el-card>
		<!-- 修改用户的对话框 -->
		<el-dialog title="修改商品" @close="editDialogclosed" :visible.sync="editDialogVisible" width="50%">
			<el-form :model="editForm" :rules="editFormrules" ref="editFormRef" label-width="70px">
				<el-form-item label="商品名称" prop="goods_name">
					<el-input v-model="editForm.goods_name"></el-input>
				</el-form-item>
				<el-form-item label="价格" prop="goods_price">
					<el-input type="number" v-model="editForm.goods_price"></el-input>
				</el-form-item>
				<el-form-item label="数量" prop="goods_number">
					<el-input v-model="editForm.goods_number"></el-input>
				</el-form-item>
				<el-form-item label="重量" prop="goods_weight">
					<el-input v-model="editForm.goods_weight"></el-input>
				</el-form-item>

			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="editDialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="edituserinfo">确 定</el-button>
			</span>
		</el-dialog>


	</div>
</template>

<script>
	export default {
		data() {
			return {
				//获取用户列表参数对象
				queryinfo: {
					query: '',
					//当前的页数
					pagenum: 1,
					//当前每页显示多少条数据
					pagesize: 5
				},
				// 存放用户列表数据
				goodslist: [],
				// 控制显示与隐藏
				editDialogVisible: false,
				//数据总条数
				total: 0,
				// 编辑商品按钮表单
				editForm: {

				},
				//修改商品表单验证对象
				//添加用户表单验证的验证规则对象
				addFormRules: {
					goods_name: [{
							required: true,
							message: '请输入商品名称',
							trigger: 'blur'
						}

					],
					goods_number: [{
							required: true,
							message: '请输入数量',
							trigger: 'blur'
						}

					],
					goods_price: [{
							required: true,
							message: '请输入价格',
							trigger: 'blur'
						}

					],
					goods_weight: [{
							required: true,
							message: '请输入重量',
							trigger: 'blur'
						}

					]
				}
			}
		},
		created() {
			this.getgoodslist()
			console.log(this.goodslist)
		},
		methods: {
			async getgoodslist() {
				const {
					data: res
				} = await this.$http.get('goods', {
					params: this.queryinfo
				})
				if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
				this.goodslist = res.data.goods
				this.total = res.data.total
			},
			//监听pagesize改变的事件
			handleSizeChange(newSize) {
				this.queryinfo.pagesize = newSize
				this.getgoodslist()()
			},
			//监听页面值改变的事件
			handleCurrentChange(newPage) {
				this.queryinfo.pagenum = newPage
				this.getgoodslist()()
			},
			// 删除商品
			async removegoodsById(id) {
				const confirmResult = await this.$confirm('此操作将永久删除商品, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					//如果用户取消删除则 则打印cancel字符串
					.catch(err => err)
				//如果用户确认删除则confirmResult 为confim字符串

				if (confirmResult != 'confirm') {
					return this.$message.info("已经取消删除")
				}
				const {
					data: res
				} = await this.$http.delete('goods/' + id)
				if (res.meta.status != 200) {
					this.$message.error("删除失败" + res.meta.msg)
				}
				this.$message.success("删除成功")
				this.getgoodslist()();
			},
			// 跳转到商品添加页面
			goAddpage() {
				this.$router.push('/goods/add')
			},
			//展示编辑用户对话框
			async showEditDialog(id) {
				const {
					data: res
				} = await this.$http.get(`goods/${id}`)
				if (res.meta.status != 200) {
					return this.$message.error("查询商品数据失败")
				}
				this.editForm = res.data
				this.editDialogVisible = true
				console.log(this.editForm)
			},
			//监听用户修改对话框的关闭事件重置input框
			editDialogclosed() {
				this.$refs.editFormRef.resetFields()
			},
			//修改用商品信息并提交
			edituserinfo() {

				this.$refs.editFormRef.validate(async valid => {

					if (!valid) return
					const {
						data: res
					} = await this.$http.put('goods/' + this.editForm.goods_id,
						this.editForm
					)

					if (res.meta.status != 200) {
						return this.$message.error("更新数据失败")
					}
					//关闭对话框
					this.editDialogVisible = false
					//刷新列表
					this.getgoodslist()
					//提示成功
					return this.$message.success("更新成功")

				})
			}



		}
	}
</script>

<style lang="less" scoped>

</style>
